#[derive(Debug, Copy, Clone)]
struct YearMonth {
    year: i32,
    month: i32,
}

struct Person {
    birth_data: YearMonth,
    original_data: YearMonth,
    new_data: YearMonth,
}

enum RetireType {
    Human50,
    Human55,
    Man,
}

// 计算原退休年龄
fn origin_retire(birth_data: &YearMonth, person_type: &RetireType) -> YearMonth {
    match person_type {
        RetireType::Human50 => YearMonth {
            year: birth_data.year + 50,
            month: birth_data.month,
        },
        RetireType::Human55 => YearMonth {
            year: birth_data.year + 55,
            month: birth_data.month,
        },
        RetireType::Man => YearMonth {
            year: birth_data.year + 60,
            month: birth_data.month,
        },
        // _ => YearMonth { year: 0, month: 0 },
    }
}

fn new_retire(birth_data: &YearMonth, person_type: &RetireType) -> YearMonth {
    let origin_data = origin_retire(birth_data, person_type);
    let month = (origin_data.year as i32) * 12i32 + (origin_data.month as i32) - 2025 * 12;
    if month < 0 {
        return origin_data;
    }
    let mut new_data = match person_type {
        RetireType::Human50 => {
            // 推迟退休月份
            let mut delay_month = month / 2;
            if delay_month > 60 {
                delay_month = 60;
            }
            let new_month = origin_data.year * 12 + origin_data.month + delay_month;
            YearMonth {
                year: new_month / 12,
                month: new_month % 12,
            }
        }
        RetireType::Human55 => {
            // 推迟退休月份
            let mut delay_month = month / 4;
            if delay_month > 36 {
                delay_month = 36;
            }
            let new_month = origin_data.year * 12 + origin_data.month + delay_month;
            YearMonth {
                year: new_month / 12,
                month: new_month % 12,
            }
        }
        RetireType::Man => {
            // 推迟退休月份
            let mut delay_month = month / 4;
            if month % 4 != 0 {
                delay_month += 1;
            }
            if delay_month > 36 {
                delay_month = 36;
            }
            let new_month = origin_data.year * 12 + origin_data.month + delay_month;
            YearMonth {
                year: new_month / 12,
                month: new_month % 12,
            }
        } // _ => YearMonth { year: 0, month: 0 },
    };

    if new_data.month == 0 {
        new_data.month = 12;
        new_data.year -= 1;
    }
    new_data
}

// 延迟退休月份
fn retire_month(person: &Person) -> String {
    let retire_month = person.new_data.year * 12 + person.new_data.month
        - person.original_data.year * 12
        - person.original_data.month;
    let retire_age = (person.new_data.year * 12 + person.new_data.month
        - person.birth_data.year * 12
        - person.birth_data.month) as f32
        / 12.0;
    format!(
        "{}-{:02},{:.2},{}",
        person.new_data.year, person.new_data.month, retire_age, retire_month
    )
}

pub fn retire_time(time: &str, tp: &str) -> String {
    let birth_str: Vec<&str> = time.split('-').collect();
    let birth_year = birth_str[0].parse::<i32>().unwrap();
    let birth_month = birth_str[1].parse::<i32>().unwrap();
    let birth_data = YearMonth {
        year: birth_year,
        month: birth_month,
    };
    let person_type = tp;

    let person_type = match person_type {
        "原法定退休年龄55周岁女职工" => RetireType::Human55,
        "原法定退休年龄50周岁女职工" => RetireType::Human50,
        "男职工" => RetireType::Man,
        _ => RetireType::Man,
    };

    let person: Person = Person {
        birth_data,
        original_data: origin_retire(&birth_data, &person_type),
        new_data: new_retire(&birth_data, &person_type),
    };

    // 确定法定退休年龄
    let law_retire = retire_month(&person);

    law_retire.replace(".00", "")
}
